home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PsL Monthly 1993 December
/
PSL Monthly Shareware CD-ROM (December 1993).iso
/
prgmming
/
win
/
pascal
/
bezier.exe
/
BEZ.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1992-02-18
|
16KB
|
579 lines
{**** Bezier 1.0 Doug Overmyer ********}
program Bez;
{$R BEZ.RES}
uses WinTypes, WinProcs, WObjects,Strings,Icons;
const
BZ_Name = 'BΘzier Demo';
idm_BZChange = 301;
idm_BZShowHide=302;
um_ReSize = 401;
id_IG1 = 600;
id_Icon1 = 601;
id_Icon2 = 602;
id_Icon3 = 603;
id_Icon4 = 604;
id_Icon5 = 605;
id_Icon6 = 606;
id_Icon7 = 607;
id_Icon8 = 608;
bs_Custom = 99;
IWidth = 32;
BEZ_DEPTH = 5;
NUM_BEZPTS = 33;
st_NotStart = 0;
st_DragHand1 = 1;
st_WaitCtrl2 = 2;
st_DragHand2 = 3;
md_DrawBezier = 1;
{********************** TYPES ******************************}
type
TBZApp = object(TApplication)
procedure InitMainWindow; virtual;
end;
PBezPoint = ^TBezPoint;
TBezPoint = object(TObject)
C1,H1,H2,C2:TPoint;
constructor Init(NewC1,NewH1,NewH2,NewC2:TPoint);
end;
PBZToolbar = ^TBZToolbar;
TBZToolbar = object(TWindow)
Icon:Array[0..8] of PIcon;
IG1:PIconGroup;
Orientation:Integer;
constructor Init(AParent:PWindowsObject;ATitle:PChar);
destructor Done;virtual;
procedure WMDrawItem(var Msg:TMessage);virtual wm_First+wm_DrawItem;
procedure UMReSize(var Msg: TMessage); virtual wm_User + um_ReSize;
procedure WMCommand(Var Msg:TMessage);virtual wm_First+wm_Command;
procedure WMNCLButtonDblClk(var Msg:TMessage);virtual wm_First+
wm_NCLButtonDblClk;
procedure ToggleOrientation;
end;
PBezTool = ^TBezTool;
TBezTool = object
DC:HDc;
InPt:TPoint;
LDFact:Integer;
State:Integer;
Ctrl1,Hand1,Hand2,Ctrl2:TPoint;
BezPts:Array[0..NUM_BEZPTS] of TPoint;
BezPtsIndx:Integer;
constructor Init;
destructor Done;virtual;
procedure SetMode(HWin:HWnd; Msg:TMessage);
procedure WMLButtonDown(HWin:Hwnd;var Msg:TMessage);virtual;
procedure WMRButtonDown(HWin:HWnd;var Msg:TMessage);virtual;
procedure WMMouseMove(HWin:HWnd;var Msg:TMessage);virtual;
procedure WMLButtonUp(HWin:HWnd;var Msg:TMessage);virtual;
procedure DrawBez(TheDC:HDC;C1,H1,H2,C2:TPoint);virtual;
procedure SubDivideBez(p0,p1,p2,p3:TPoint;BezDepth:Integer);virtual;
procedure DrawHandle(TheDC:HDc;p,q:TPoint);virtual;
end;
PBZWindow = ^TBZWindow;
TBZWindow = object(TWindow)
ToolBar:PBZToolbar;
BezTool:PBezTool;
lbColor:TColorRef;
ColorIndx:Integer;
RedPen,BezPen:HPen;
Picture:PCollection;
Mode:Integer;
Cross,Arrow:HCursor;
constructor Init(ATitle: PChar);
destructor Done; virtual;
procedure SetupWindow;virtual;
procedure WMSize(var Msg: TMessage); virtual wm_First + wm_Size;
procedure IDIcon1(Var Msg:TMessage);virtual wm_User+id_Icon1;
procedure IDIcon2(Var Msg:TMessage);virtual wm_User+id_Icon2;
procedure IDIcon3(Var Msg:TMessage);virtual wm_User+id_Icon3;
procedure IDIcon4(Var Msg:TMessage);virtual wm_User+id_Icon4;
procedure WMLButtonDown(var Msg:TMessage);virtual wm_First+wm_LButtonDown;
procedure WMRButtonDown(var Msg:TMessage);virtual wm_First+wm_rButtonDown;
procedure WMMouseMove(var Msg:TMessage);virtual wm_First+wm_MouseMove;
procedure WMLButtonUp(var Msg:TMessage);virtual wm_First+wm_LButtonUp;
procedure Paint(PaintDC:HDC;var PaintInfo:TPaintStruct);virtual;
procedure DrawCoords(ADC:HDC);
end;
{********************** GLOBALS ******************************}
var
MainWin:PBZWindow;
function Tenary(Exp:Boolean;T,F:LongInt):LongInt;
begin
if Exp then Tenary := T else Tenary := F;
end;
{********************** METHODS ******************************}
procedure TBZApp.InitMainWindow;
begin
MainWindow := New(PBZWindow, Init(BZ_Name));
MainWin := PBZWindow(MainWindow);
end;
{********************** TBZWindow *******************************}
constructor TBZWindow.Init(ATitle: PChar);
begin
TWindow.Init(nil, ATitle);
with Attr do
begin
X := 50; Y := 50; W := 500; H := 300;
end;
ToolBar := New(PBZToolbar,Init(@Self,'Tools'));
BezTool := New(PBezTool,Init);
lbColor := RGB(0,0,0);
ColorIndx := 0;
RedPen := CreatePen(ps_Solid,1,RGB(255,0,0));
BezPen := CreatePen(ps_Solid,1,RGB(0,0,0));
Picture := New(PCollection,Init(10,10));
Mode := 0;
end;
destructor TBZWindow.Done;
var
Msg:TMessage;
begin
TWindow.Done;
Dispose(BezTool,Done);
if BezPen <> 0 then
DeleteObject(BezPen);
DeleteObject(RedPen);
Dispose(Picture,Done);
end;
procedure TBZWindow.SetupWindow;
var
SysMenu:HMenu;
begin
TWindow.SetupWindow;
SetClassWord(HWindow,GCW_HIcon,LoadIcon(HInstance,'BZ_Icon'));
if ToolBar <> nil then
SendMessage(ToolBar^.HWindow,wm_User+um_ReSize,0,0);
Cross := LoadCursor(0,idc_Cross);
Arrow := LoadCursor(0,idc_Arrow);
end;
procedure TBZWindow.WMSize(var Msg: TMessage);
begin
TWindow.WMSize(Msg);
if ToolBar <> nil then {optional follow-along}
SendMessage(ToolBar^.HWindow,wm_User+um_ReSize,0,0);
end;
procedure TBZWindow.IDIcon1(var Msg:TMessage);
begin
Mode := md_DrawBezier;
Cross := LoadCursor(0,idc_Cross);
SetClassWord(HWindow,gcw_HCursor,Cross);
end;
procedure TBZWindow.IDIcon2(var Msg:TMessage);
var
C:TPoint;
begin
Mode := 0;
SetClassWord(HWindow,gcw_HCursor,Arrow);
Picture^.FreeAll;
C.X := 0; C.Y := 0;
InvalidateRect(HWindow,nil,True);
end;
procedure TBZWindow.IDIcon3(var Msg:TMessage);
begin
Mode := 0;
SetClassWord(HWindow,gcw_HCursor,Arrow);
Inc(ColorIndx);
If ColorIndx > 9 then ColorIndx := 0;
case ColorIndx of
0:lbColor := RGB(0,0,0);
1:lbColor := RGB(0,0,255);
2:lbColor := RGB(255,0,0);
3:lbColor := RGB(255,0,255);
4:lbColor := RGB(0,255,0);
5:lbColor := RGB(0,255,255);
6:lbColor := RGB(255,255,0);
7:lbColor := RGB(255,255,255);
8:lbColor := RGB(192,192,192);
9:lbColor := RGB(128,128,128);
end;
if BezPen <> 0 then
DeleteObject(BezPen);
BezPen := CreatePen(ps_Solid,1,lbColor);
end;
procedure TBZWindow.IDIcon4(var Msg:TMessage);
begin
CloseWindow;
end;
procedure TBZWindow.WMLButtonDown(var Msg:TMessage);
begin
Case Mode of
md_DrawBezier:BezTool^.WMLButtonDown(HWindow,Msg);
end;
end;
procedure TBZWindow.WMRButtonDown(var Msg:TMessage);
begin
Picture^.FreeAll;
InvalidateRect(HWindow,nil,True);
end;
procedure TBZWindow.WMMouseMove(var Msg:TMessage);
begin
case Mode of
md_DrawBezier: BezTool^.WMMouseMove(HWindow,Msg);
end;
end;
procedure TBZWindow.WMLButtonUp(var Msg:TMessage);
var
ADC:HDC;
begin
case Mode of
md_DrawBezier:
begin
BezTool^.WMLButtonUp(HWindow,Msg);
ADC := GetDC(HWindow);
DrawCoords(ADC);
ReleaseDC(HWindow,ADC);
end;
end;
end;
procedure TBZWindow.Paint(PaintDC:HDC;var PaintInfo:TPaintStruct);
var
Indx:Integer;
BP:PBezPoint;
begin
DrawCoords(PaintDC);
SetMapMode(PaintDC,mm_Twips);
if Picture^.Count > 0 then
for Indx := 0 to (Picture^.Count -1) do
begin
BP := Picture^.At(Indx);
BezTool^.DrawBez(PaintDC,BP^.C1,BP^.H1,BP^.H2,BP^.C2);
end;
end;
procedure TBZWindow.DrawCoords(ADC:HDC);
var
BezPts : Record
C1X,C1Y,H1X,H1Y,C2X,C2Y,H2X,H2Y:Integer;
end;
OutStr:Array[0..255] of Char;
Indx:Integer;
BP:PBezPoint;
begin
StrCopy(OutStr,'');
FillChar(OutStr,250,' ');
TextOut(ADC,5,5,OutStr,StrLen(OutStr));
if Picture^.Count > 0 then
begin
BP := Picture^.At(Picture^.Count-1);
BezPts.C1X := BP^.C1.x;
BezPts.C1Y := BP^.C1.Y;
BezPts.H1X := BP^.H1.x;
BezPts.H1y := BP^.H1.y;
BezPts.C2X := BP^.C2.x;
BezPts.C2y := BP^.C2.y;
BezPts.H2X := BP^.H2.x;
BezPts.H2y := BP^.H2.y;
end
else
begin
BezPts.C1X := 0;
BezPts.C1Y := 0;
BezPts.H1X := 0;
BezPts.H1y := 0;
BezPts.C2X := 0;
BezPts.C2y := 0;
BezPts.H2X := 0;
BezPts.H2y := 0;
end;
wvsprintf(OutStr,'C1(%i,%i) H1(%i,%i) C2(%i,%i) H2(%i,%i)',BezPts);
TextOut(ADC,5,5,OutStr,StrLen(OutStr));
end;
{********************** TBezTool *****************************}
constructor TBezTool.Init;
begin
DC := 0;
State := 0;
Ctrl1.x := 0;Ctrl1.y := 0;Hand1.x :=0;Hand1.Y := 0;
C